/* 
多态
*/

(function () {
  class Animal {
    name: string
    
    constructor (name: string) {
      this.name = name
    }

    run (distance: number=0) {
      console.log(`${this.name} run ${distance}m`)
    }

  }

  class Snake extends Animal {
    constructor (name: string) {
      // 调用父类型构造方法
      super(name)
    }

    // 重写父类型的方法
    run (distance: number=5) {
      console.log('sliding...')
      super.run(distance)
    }
  }

  class Horse extends Animal {
    constructor (name: string) {
      // 调用父类型构造方法
      super(name)
    }

    // 重写父类型的方法
    run (distance: number=50) {
      console.log('dashing...')
      // 调用父类型的一般方法
      super.run(distance)
    }

    xxx () {
      console.log('xxx()')
    }
  }

  const a1: Animal = new Animal('a1')
  const a2: Animal = new Snake('s1')  // 父类型引用指向子类型对象
  const a3: Animal = new Horse('h1')  // 父类型引用指向子类型对象

  function fn(a: Animal) {
    a.run()
  }
  fn(a2)
  fn(a3)
  
})()